﻿using CBS.APP;
using CBS.DB.CBSEntity;
using CBS.Model.ManageModel;
using CBS.Model.QueryModel;
using CBS.Model.ViewModel;
using CBSWeb.Server.App_Start;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace CBSWeb.Server.Controllers
{
    /// <summary>
    /// 学期相关接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SemesterController : ControllerBase
    {
        private readonly ILogger<SemesterController> _logger;
        public SemesterController(ILogger<SemesterController> logger)
        {
            _logger = logger;
        }


        /// <summary>
        /// 获取学期列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetSemesterList([FromQuery] Pagination pagination, string? keyword)
        {
            var data = new
            {
                rows = SemesterApp.GetList(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };

            return BaseJsonResult.Success(data);
        }

        /// <summary>
        /// 获取学期详情
        /// </summary>
        /// <param name="semesterId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get(SemesterIdQuery semesterId)
        {

            return BaseJsonResult.Success(SemesterApp.GetForm(semesterId.semesterId));

        }

        /// <summary>
        /// 新增学期数据
        /// </summary>
        /// <param name="semester"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SubmitSemester(SubmitSemesterQuery semester)
        {
            if (string.IsNullOrWhiteSpace(semester.semesterName))
            {

            }
            SemesterViewModel semesterView = new SemesterViewModel() 
            {
                SemesterName = semester.semesterName,
                Memo = semester.Memo,
                StartTime = semester.StartTime,
                EndTime = semester.EndTime
            };

            if (SemesterApp.CreateSemester(semesterView))
            {
                return BaseJsonResult.Success();
            }
            else
            {
                return BaseJsonResult.Error();
                _logger.Log(LogLevel.Error, "", semester);
            }
        }

        /// <summary>
        /// 删除学期数据
        /// </summary>
        /// <param name="semester"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteSemester(DeleteSemesterQuery semester)
        {


            if (string.IsNullOrWhiteSpace(semester.semesterName))
            {

            }
            SemesterViewModel semesterView = new SemesterViewModel();

            if (SemesterApp.DeleteSemester(semester.semesterId))
            {
                return BaseJsonResult.Success();
            }
            else
            {
                return BaseJsonResult.Error();
            }
        }
    }
}
